package com.material.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.material.common.annotation.LoginUser;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.DateUtils;
import com.material.factory.domain.FactoryNotice;
import com.material.factory.service.IFactoryNoticeService;
import com.material.request.BaseRequest;
import com.material.request.FactoryNoticeQueryRequest;
import com.material.supplier.domain.SupplierInfo;
import com.material.supplier.service.ISupplierInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ganxinge
 */
@Api(tags = "11.通知")
@RestController
@RequestMapping("/notice")
public class FactoryNoticeController {

    @Autowired
    private IFactoryNoticeService factoryNoticeService;
    @Autowired
    private ISupplierInfoService supplierInfoService;

    @ApiOperation("通知列表")
    @GetMapping("/list")
    public ResultResponse list(@ApiIgnore @LoginUser SysUser sysUser, FactoryNoticeQueryRequest params){

        Long factoryId = 0L;
        String userType = sysUser.getUserType();
        if (SysUser.USER_TYPE_3.equals(userType)){
            Long supplierId = sysUser.getBizId();
            SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(supplierId);
            factoryId = supplierInfo.getFactoryId();
        }
        if(SysUser.USER_TYPE_1.equals(userType) || SysUser.USER_TYPE_2.equals(userType)){
            factoryId = sysUser.getBizId();
        }
        FactoryNotice query = new FactoryNotice();
        query.setFactoryId(factoryId);
        PageHelper.startPage(params.getPageNum(),params.getPageSize(),"id desc");
        PageInfo<FactoryNotice> pageInfo = factoryNoticeService.selectFactoryNoticePageList(query);
        return ResultResponse.success(pageInfo);
    }

    @ApiOperation("今日通知")
    @GetMapping("/todayNotice")
    public ResultResponse todayNotice(@ApiIgnore @LoginUser SysUser sysUser){
        Long factoryId = 0L;
        String userType = sysUser.getUserType();
        if (SysUser.USER_TYPE_3.equals(userType)){
            Long supplierId = sysUser.getBizId();
            SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(supplierId);
            factoryId = supplierInfo.getFactoryId();
        }
        if(SysUser.USER_TYPE_1.equals(userType) || SysUser.USER_TYPE_2.equals(userType)){
            factoryId = sysUser.getBizId();
        }
        String currentDate = DateUtils.getDate();
        FactoryNotice factoryNotice = factoryNoticeService.selectFactoryNoticeByDate(currentDate,factoryId);
        if(factoryNotice == null){
            return ResultResponse.success();
        }
        return ResultResponse.success(factoryNotice);
    }


}
